package v1

import (
	"com.cyt.common/library/util"
	"com.cyt.common/model"
	"com.cyt.common/model/membermodel"
	"com.cyt.common/service/memberservice"
	"fmt"
	"github.com/gogf/gf/frame/g"
)

// 会员API对象
var Member = new(memberApi)

type memberApi struct {
	Controller
	MemberModel   membermodel.MemberModel
	MemberService memberservice.MemberService
}

// 创建会员
func (c *memberApi) MemberCreate() {
	var req *membermodel.MemberCreateReq
	// 绑定验证
	c.Valid(&req)
	err := c.MemberService.SaveMember(req)
	if err != nil {
		util.WriteLog("member/create_fail", err.Error())
		c.Error("会员创建失败")
	}
	c.Success("成功")
}

// 修改会员
func (c *memberApi) MemberUpdate() {
	var req *membermodel.MemberUpdateReq
	// 绑定验证
	c.Valid(&req)
	err := c.MemberService.UpdateMember(req)
	if err != nil {
		util.WriteLog("member/update_fail", err.Error())
		c.Error("会员修改失败")
	}
	c.Success("成功")
}

// 删除会员
func (c *memberApi) MemberDelete() {
	var req *model.GetById
	// 绑定验证
	c.Valid(&req)
	err := c.MemberService.DeleteMember(req.Id)
	if err != nil {
		util.WriteLog("member/delete_fail", err.Error())
		c.Error("会员删除失败")
	}
	c.Success("成功")
}

// 会员信息
func (c *memberApi) MemberInfo() {
	MemberId := c.Request.GetInt("member_id")
	if MemberId <= 0 {
		c.Error("参数错误")
	}
	memberInfo, err := c.MemberModel.FindOneByCondition(g.Map{"id": MemberId})
	if err != nil {
		c.Error("获取会员信息失败")
	}
	c.Success("成功", memberInfo)
}

// 会员列表
func (c *memberApi) MemberList() {
	var req *membermodel.MemberListReq
	// 绑定验证
	c.Valid(&req)
	fmt.Println(c.Request.GetMap())
	result, err := c.MemberModel.FindAllByParam(req)
	if err != nil {
		c.Error("获取会员列表失败")
	}
	c.Paginate(result)
}
